<template>
  <div class="tab-bar-item"
       @click="itemClick">
    <slot name="item-icon"
          v-if="!isActive"></slot>
    <slot name="item-icon-active"
          v-else></slot>
    <div :style="activeStyle">
      <slot name="item-text"></slot>
    </div>

  </div>
</template>

<script>
export default {
  name: 'TabBarItem',
  props: {
    path: String,
    activeColor: {
      type: String,
      default: 'red'
    }
  },
  data () {
    return {

    }
  },
  methods: {
    itemClick () {
      this.$router.push(this.path).catch(err => err);
    }
  },
  computed: {
    //当前tabbaritem是否处于活跃状态
    isActive () {
      return this.$route.path.indexOf(this.path) !== -1;
    },
    //处于活跃状态的item颜色
    activeStyle () {
      return this.isActive ? { color: this.activeColor } : {};
    }
  }
}
</script>

<style lang="less" scoped>
.tab-bar-item {
  flex: 1;
  font-size: 14px;
  img {
    width: 25px;
    height: 25px;
    margin-top: 3px;
    margin-bottom: 2px;
    vertical-align: middle;
  }
}

.active {
  color: red;
}
</style>